package com.khaled.protclass.model.feature.physicochemical;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.log4j.Logger;

import com.khaled.protclass.model.base.BaseFeature;
import com.khaled.protclass.model.base.IProtein;
import com.khaled.protclass.model.bean.weka.Attribute;
import com.khaled.protclass.model.bean.weka.WekaDataTypes;
import com.khaled.protclass.model.util.UtilAminoAcids;
import com.khaled.protclass.model.util.UtilNumber;

public abstract class AbstractPhysicoChemical extends BaseFeature
{
	/** Logger. */
	private static Logger logger = Logger.getLogger(AbstractPhysicoChemical.class);

	@Override
	public void init()
	{
		// TODO Auto-generated method stub
	}

	public ArrayList<Attribute> getAttributes(String property)
	{
		logger.info("getAttributes of " + property + " feature starts.");

		ArrayList<Attribute> attributes = new ArrayList<Attribute>();

		for (String amino : UtilAminoAcids.getInstance().getAminoAcidNames())
		{
			attributes.add(new Attribute(property + "_" + amino, WekaDataTypes.NUMERIC));
		}
		logger.info("Number of attributes:" + attributes.size());
		logger.info("getAttributes of " + property + " feature ends.");
		return attributes;
	}

	public String getFeatureVector(IProtein protein, HashMap<String, Double> attribute)
	{
		logger.info("getFeatureVector starts.");

		StringBuffer featureVector = new StringBuffer();
		String sequence = protein.getProteinSequence();

		logger.info("Protein ID: " + protein.getProteinID());
		logger.info("Protein Classification:" + protein.getClassification());
		logger.info("Protein Sequence:" + protein.getProteinSequence());

		for (String amino : UtilAminoAcids.getInstance().getAminoSymbols())
		{
			double sum = 0;
			for (int i = 0; i < sequence.length(); i++)
			{
				String seq = Character.toString(sequence.charAt(i));
				if (amino.equals(seq))
				{
					sum += attribute.get(amino);
				}
			}
			double featureValue = sum / (double) sequence.length();
			logger.info("Summation of physico-chemical property of Amino Acid (" + amino + ") = " + sum);
			logger.info("Avg of physico-chemical property of Amino Acid (" + amino + ") = " + featureValue);

			featureVector.append(UtilNumber.round(featureValue, 2)).append(",");
		}
		logger.info("Feature Vector:" + featureVector);

		return featureVector.toString();
	}

}
